const path = require("path");
const log4js = require("koa-log4");

log4js.configure({
  appenders: {
    access: {
      // 访问日志
      type: "dateFile",
      pattern: "-yyyy-MM-dd.log",
      filename: path.join("./logs/", "access.log"),
      alwaysIncludePattern: true,
      encoding: "utf-8",
    },
    application: {
      // 系统日志
      type: "dateFile",
      pattern: "-yyyy-MM-dd.log",
      filename: path.join("./logs/", "application.log"),
      alwaysIncludePattern: true,
      encoding: "utf-8",
    },
    out: { type: "console" }, // 控制台输出
  },
  categories: {
    default: { appenders: ["out"], level: "info" }, // 默认日志
    access: { appenders: ["access"], level: "info" }, // 记录所有访问请求
    application: { appenders: ["application"], level: "WARN" }, // 记录警告及以上级别日志
  },
});

// 导出日志中间件和系统日志实例
exports.accessLogger = () => log4js.koaLogger(log4js.getLogger("access"));
exports.systemLogger = log4js.getLogger("application");
